package com.haidong.service;

import com.haidong.mapper.BrandMapper;
import com.haidong.pojo.Brand;
import com.haidong.util.SQLSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

//负责数据查询
public class BrandService {
    SqlSessionFactory factory = SQLSessionFactoryUtils.getSqlSessionFactory();

    /**
     *  查询所有信息
     * @return
     */
    public List<Brand> selectAll(){
        SqlSession sqlSession = factory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = brandMapper.selectAll();
        sqlSession.close();
        return brands;
    }

    /**
     * 添加
     * @param brand
     */
    public void add(Brand brand){
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.add(brand);
        sqlSession.commit();
        sqlSession.close();
    }
}
